package com.javaee.tallybook.controller;

import com.javaee.tallybook.pojo.Tally;
import com.javaee.tallybook.service.TallyService;
import com.javaee.tallybook.until.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tal")
public class TallyController {

    @Autowired
    private TallyService tallyService;

    /**
     * 获取根据用户名账单列表
     * @param session
     * @return
     */
    @GetMapping("/list")
    public Map<String,Object> list(
            HttpSession session
    ){
        JsonResult jsonResult=new JsonResult();
        String name = (String) session.getAttribute("name");
        List<Tally> list = tallyService.getList(name);
        jsonResult.setData(list);
        jsonResult.setCode(0);
        System.out.println(jsonResult.getValues());
        return jsonResult.getValues();
    }

    /**
     * 添加账单
     * @param type
     * @param money
     * @param des
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add(
            @RequestParam("type") String type,
            @RequestParam("money") String money,
            @RequestParam("des") String des,
            HttpSession session
    ){
        JsonResult jsonResult=new JsonResult();
        Date now=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String time=format.format(now);
        String name = (String) session.getAttribute("name");
        Double mon=Double.valueOf(money);
        Tally tally=new Tally();
        tally.setU_name(name);
        tally.setType(type);
        tally.setMoney(mon);
        tally.setDes(des);
        tally.setTime(time);
        Integer result = tallyService.addList(tally);
        if (result>0){
            jsonResult.setCode(1);
            return new ModelAndView("redirect:/add.html");
        }else {
            jsonResult.setCode(0);
            return new ModelAndView("添加失败");
        }
    }

    @RequestMapping("/del")
    public ModelAndView del(
            @RequestParam("id") Integer id
    ){
        JsonResult jsonResult=new JsonResult();
        Integer result = tallyService.delList(id);
        if (result>0){
            jsonResult.setCode(1);
            return new ModelAndView("redirect:/list.html");
        }else {
            jsonResult.setCode(0);
            return new ModelAndView("redirect:/list.html");
        }
    }

    @RequestMapping("/update")
    public ModelAndView update(
            @RequestParam("id") Integer id,
            @RequestParam("money") Double money
    ){
        Tally tally=new Tally();
        tally.setT_id(id);
        tally.setMoney(money);
        Integer result = tallyService.update(tally);
        if (result>0){
            return new ModelAndView("redirect:/list.html");
        }else {
            return new ModelAndView("redirect:/list.html");
        }
    }

    @RequestMapping("/count")
    public Map<String,Object> count(
            HttpSession session
    ){
        String name = (String) session.getAttribute("name");
        JsonResult jsonResult=new JsonResult();
        Double in = tallyService.countIn(name);
        Double out = tallyService.countOut(name);
        jsonResult.put("in",in);
        jsonResult.put("out",out);
        return jsonResult.getValues();
    }
}
